package com.seele.tigerwallet.ui.activtiy;


import android.annotation.TargetApi;
import android.app.ActivityOptions;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;

import com.seele.tigerwallet.R;
import com.seele.tigerwallet.model.AssetsModel;
import com.seele.tigerwallet.ui.base.TitleBaseActivity;
import com.seele.tigerwallet.ui.base.ToolbarBaseActivity;
import com.seele.tigerwallet.ui.callback.OnSelectListener;
import com.seele.tigerwallet.ui.event.AssetsEvent;
import com.seele.tigerwallet.ui.utils.GsonUtil;
import com.seele.tigerwallet.ui.utils.KeyBoardUtils;
import com.seele.tigerwallet.ui.views.AddAssetsRecyclerView;
import com.seele.tigerwallet.ui.views.AssetsSearchRecyclerView;
import com.seele.tigerwallet.ui.views.SearchEditext;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Description:添加资产界面
 * Data: 2018/6/11 11:24
 * author:
 */
public class AddAssetsActivity extends ToolbarBaseActivity {

    private AddAssetsRecyclerView mRecyclerView;
    private AssetsSearchRecyclerView mSearchRecyclerView;
    private List<AssetsModel> mBundleList;//传值

    private String keyword;
    private long mSearchTime;

    @Override
    public int getContainerViewId() {
        return R.layout.activity_add_assets;
    }

    @Override
    protected void initView() {
        setTitle(R.string.add_assets);
        mRecyclerView = $f(R.id.add_assets_recyclerView);
        mSearchRecyclerView = $f(R.id.search_recyclerView);
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            mBundleList = (List<AssetsModel>) bundle.getSerializable(AddAssetsActivity.class.getSimpleName());
        }
        setRecyclerView();
        setNormal();


    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onRightImg() {
        addSearchView(getResources().getString(R.string.sear_hint));
        //搜索View 设置一个空View,弹出键盘
        mSearchEditext.setFocusable(true);
        mSearchEditext.requestFocus();
        KeyBoardUtils.openKeybord(mSearchEditext,this);
        setNormalSearchRecyclerView();

    }

    /**
     * 设置搜索的默认状态
     */
    private void setNormalSearchRecyclerView() {
        mRecyclerView.setVisibility(View.GONE);
        mSearchRecyclerView.setVisibility(View.VISIBLE);
        mSearchRecyclerView.setNormal();

    }

    @Override
    protected void onRightText() {
        KeyBoardUtils.closeKeybord(mSearchEditext,this);
        setNormal();
        setRecyclerView();



    }

    private void setNormal() {
        setTitle(R.string.add_assets);
        setRightImg(R.drawable.search_select);
        setNormalBar();
    }

    /**
     * 设置基本的RecyclerView
     */
    public void setRecyclerView() {
        mSearchRecyclerView.setVisibility(View.GONE);
        mRecyclerView.setVisibility(View.VISIBLE);
        mRecyclerView.setBundleList(mBundleList, true);
        mRecyclerView.setListener(new OnSelectListener<AssetsModel>() {
            @Override
            public void onSelect(List<AssetsModel> assetsModelList) {
                mBundleList = assetsModelList;
                mRecyclerView.notifyDataSetChanged();

            }
        });

    }

    public void sreachRecyclerView(String keyword) {
        mRecyclerView.setVisibility(View.GONE);
        mSearchRecyclerView.setVisibility(View.VISIBLE);
        mSearchRecyclerView.setBundleList(mBundleList, keyword);
        mSearchRecyclerView.setListener(new OnSelectListener<AssetsModel>() {
            @Override
            public void onSelect(List<AssetsModel> assetsModelList) {
                mBundleList = assetsModelList;
            }
        });
    }


    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if (TextUtils.isEmpty(charSequence)) {
            sreachRecyclerView(null);
        } else if ((System.currentTimeMillis() - mSearchTime) > 300) {
            if (!TextUtils.isEmpty(charSequence)) {
                keyword = charSequence.toString();
                mSearchTime = System.currentTimeMillis();
                sreachRecyclerView(keyword);
            }
        }
    }

    @Override
    protected void onPause() {
        if (null != mBundleList && mBundleList.size() > 0) {
            AssetsEvent assetsEvent = new AssetsEvent(mBundleList);
            EventBus.getDefault().post(assetsEvent);
        }
        super.onPause();
    }


}
